<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      
      <el-form-item label="活动名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择活动开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择活动结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activity:activitys:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activity:activitys:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activity:activitys:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:activitys:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activitysList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" :index="createIndex" />
       <el-table-column label="活动名称" align="center" prop="name">
        <template slot-scope="scope">
          <span @click="viewName(scope.row)">{{scope.row.name}}</span>
        </template>
       </el-table-column>
      <el-table-column label="审批状态" align="center" prop="checkStatus">
         <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_audit_status" :value="scope.row.checkStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="pcUrl" >
        <!-- 图片的显示 -->
                 <template   slot-scope="scope">            
                    <img :src="scope.row.pcUrl"  min-width="70px" height="70px" 
                           @click="previewPic(scope.row.pcUrl)" />
                 </template>     
      </el-table-column>
      <el-table-column label="创建时间时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="报名人数" align="center" prop="applyNumber" />
      <el-table-column label="状态" align="center" prop="status">
         <template slot-scope="scope">
          <el-switch  v-model="scope.row.status"
                :active-value="0"
                :inactive-value="2"
                @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否为官方发布" align="center" prop="isGf">
         <template slot-scope="scope">
          <el-switch  v-model="scope.row.isGf"
                :active-value="0"
                :inactive-value="2"
                @change="handleStatusChangeisGf(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:activitys:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activity:activitys:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row :gutter="20">
      <el-col :span="12">
     <div class="divList">
             <el-scrollbar class="scrollbar_wrap">
        <el-form-item label="图片地址" prop="pcUrl">
           <el-upload
          class="avatar-uploader"
          ref="upload"
          :action="upload.url"
          :headers="upload.headers"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" style="width: 80px;height:80px">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        </el-form-item>
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动名称"/>
        </el-form-item>
        <el-form-item label="最大活动人数" prop="maxNumber">
          <el-input v-model="form.maxNumber" placeholder="请输入最大活动人数" />
        </el-form-item>
        <el-form-item label="轰动人数范围" prop="peopleNumber">
          <el-input v-model="form.peopleNumber" placeholder="请输入轰动人数范围" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="活动报名开始时间" prop="applyStartTime">
          <el-date-picker clearable
            v-model="form.applyStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动报名开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动报名结束时间" prop="applyEndTime">
          <el-date-picker clearable
            v-model="form.applyEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动报名结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="联系电话" prop="telPhone">
          <el-input v-model="form.telPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="活动开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收藏人数" prop="collectNumber">
          <el-input v-model="form.collectNumber" placeholder="请输入收藏人数" />
        </el-form-item>
        <el-form-item label="报名人数" prop="applyNumber">
          <el-input v-model="form.applyNumber" placeholder="请输入报名人数" />
        </el-form-item>
        <el-form-item label="地址" prop="detailedAddress">
          <el-input v-model="form.detailedAddress"/>
          <div style="width:100%;border: 2px solid #ced4da;padding: 2px;margin: 10px 0">
              <iframe id="mapPage" style="width: 100%;height: 600px;z-index: 999" frameborder=0
                    src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=5OVBZ-27I6W-5LNRB-OWUIN-FUCVF-5SF6P&referer=云社群-H5">
              </iframe>
              </div>
        </el-form-item>
        </el-scrollbar>
      </div>
        </el-col>
        <el-col :span="12">
        <el-form-item label="活动内容">
           <div class="divList">
           <el-scrollbar class="scrollbar_wrap">
          <editor v-model="form.content" :min-height="650"/>
           </el-scrollbar>
           </div>
        </el-form-item>
        </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 图片预览   放在表格外面，这是个弹出框-->
      <el-dialog :visible.sync="dialogVisible" :modal="false" title="图片预览" width="50%">
        <img :src="previewpic" alt=""  width="100%" />
        <span slot="footer" class="dialog-footer">
          </span>
       </el-dialog>
  <!-- 图片预览 -->

   <el-dialog :title="title" :visible.sync="viewOpen" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-descriptions  direction="vertical" :column="2" border>
      <el-descriptions-item label="活动名称">{{form.name}}</el-descriptions-item>
      <el-descriptions-item label="审批状态">
        <el-tag v-if="form.checkStatus==0" type="info">审批中</el-tag>
        <el-tag v-if="form.checkStatus==1" type="success">通过</el-tag>
        <el-tag v-if="form.checkStatus==2" type="warning">驳回</el-tag>
        <el-tag v-if="form.checkStatus==3" type="danger">撤销</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="活动地址" >{{form.address}}</el-descriptions-item>
      <el-descriptions-item label="报名费用">
        {{form.price}}
      </el-descriptions-item>
      <el-descriptions-item label="活动时间" >{{form.startTime}}至{{form.endTime}}</el-descriptions-item>
      <el-descriptions-item label="报名时间" >{{form.applyStartTime}}至{{form.applyEndTime}}</el-descriptions-item>
      <el-descriptions-item label="最大报名人数" >{{form.maxNumber}}</el-descriptions-item>
      <el-descriptions-item label="已报名人数" >{{form.applyNumber}}</el-descriptions-item>
      <el-descriptions-item label="联系人" >{{form.contacts}}</el-descriptions-item>
      <el-descriptions-item label="联系电话" >{{form.telPhone}}</el-descriptions-item>
      <el-descriptions-item label="活动封面"><img :src="form.pcUrl" class="imag"  @click="previewPic(form.pcUrl)"></el-descriptions-item>
      </el-descriptions>
      <el-card class="box-card">
      <div class="text item">
        <span v-html="form.content"></span>
      </div>
      </el-card>
      </el-form>
        </el-dialog>
       
  </div>
</template>

<script>
import { listActivitys, getActivitys, delActivitys, addActivitys, updateActivitys,changeNoticeStatus,changeNoticeisGf } from "@/api/activity/activitys";
import { getToken } from "@/utils/auth";
export default {
  name: "Activitys",
  dicts: ['sys_audit_status'],
  data() {
    return {
      dialogVisible: false,
      previewpic: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动表格数据
      activitysList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      viewOpen:false,
         //上传参数
      upload:{
          // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/content/press/importData"
      },
      mainVm:{},
       imageUrl: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        contacts: null,
        pcUrl: null,
        name: null,
        maxNumber: null,
        peopleNumber: null,
        price: null,
        applyStartTime: null,
        applyEndTime: null,
        telPhone: null,
        startTime: null,
        endTime: null,
        content: null,
        address: null,
        customField: null,
        checkStatus: null,
        rejectReason: null,
        approverId: null,
        checkTime: null,
        copyId: null,
        collectNumber: null,
        applyNumber: null,
        status: null,
        asId: null,
        reprintIds: null,
        reproduceIds: null,
        isGf: null,
        detailedAddress: null,
        coord: null,
        payType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        asId: [
          { required: true, message: "协会id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
mounted(){
      //接收来自其他页面的消息并判断发送者
    window.addEventListener("message", this.onMessage);

},
 
  methods: {
    createIndex(index){
         return (this.queryParams.pageNum - 1)* this.queryParams.pageSize + index +1;
    },
    //获取地址
    onMessage(event) {
	  // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
	  var loc = event.data;
	  if (loc && loc.module == 'locationPicker') {
      //防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
	      console.log('location', loc);
        if(loc.poiname=='我的位置'){
          this.$modal.confirm("请重新选择地址")
        }
        this.form.detailedAddress=loc.cityname+"·"+loc.poiname;
        let obj={};
        obj.name=loc.poiname;
        obj.coord=loc.latlng.lat+","+loc.latlng.lng;
        this.form.address=JSON.stringify(obj);
        console.info(this.form.address);
	      this.mainVm.dialogFormVisibleMap=false;
	  }
},  
   //查看具体内容
viewName(row){
  console.info(row);
    this.form=row;
    this.viewOpen=true;
    this.title="查看内容"
   },

    /** 查询活动列表 */
    getList() {
      this.loading = true;
      listActivitys(this.queryParams).then(res => {
        console.info(res);
        this.activitysList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
        //图片放大
    previewPic(url) {
           this.previewpic = url;
           this.dialogVisible = true;
        },
    btn(){},
    // 状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.id + '"公告吗？').then(function() {
        return changeNoticeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "2" : "0";
      });
    },
    // 是否官方发布修改
    handleStatusChangeisGf(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.id + '"公告吗？').then(function() {
        return changeNoticeisGf(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "2" : "0";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        contacts: null,
        pcUrl: null,
        name: null,
        maxNumber: null,
        peopleNumber: null,
        price: null,
        applyStartTime: null,
        applyEndTime: null,
        telPhone: null,
        startTime: null,
        endTime: null,
        content: null,
        address: null,
        customField: null,
        checkStatus: 0,
        rejectReason: null,
        approverId: null,
        checkTime: null,
        copyId: null,
        collectNumber: null,
        applyNumber: null,
        createTime: null,
        updateTime: null,
        status: 0,
        asId: null,
        reprintIds: null,
        reproduceIds: null,
        isGf: null,
        detailedAddress: null,
        coord: null,
        payType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivitys(id).then(response => {
        this.form = response.data;
        this.imageUrl=response.data.pcUrl;
        this.open = true;
        this.title = "修改活动";
      });
    },
    //上传图片操作
     handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
         if(res.code==200){
          this.form.pcUrl=res.data;
        }else{
          console.info(res.msg)
        }
      },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivitys(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivitys(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活动编号为"' + ids + '"的数据项？').then(function() {
        return delActivitys(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('activity/activitys/export', {
        ...this.queryParams
      }, `activitys_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.divList{

  height:650px;
 
}
.scrollbar_wrap{
  height: 100%;
  overflow-x: hidden;
}
.display-scrollbar_thumb{
  background: transparent;
}
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
 .imag{
  width: auto;
  height: 150px;
  margin-left: auto;
  margin-right: auto;
  display: flex;
 } 
</style>
